<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dynamic URL Dispatch</title>
</head>

<body>
<h1>Dynamic URL Dispatch</h1>

<p>In the <a href="static-dispatch.xhtml">previous example</a> we covered how to
statically configure Twisted Web to serve different content at different
URLs. The goal of this example is to show you how to do this dynamically
instead. Reading the previous installment if you haven't already is suggested in
order to get an overview of how URLs are treated when using Twisted Web's <code
class="API" base="twisted.web">resource</code> APIs.</p>

<p><code class="API" base="twisted.web.server">Site</code> (the object which
associates a listening server port with the HTTP implementation), <code
class="API" base="twisted.web.resource">Resource</code> (a convenient base class
to use when defining custom pages), and <code class="API"
base="twisted.internet">reactor</code> (the object which implements the Twisted
main loop) return once again:</p>

<pre class="python">
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor
</pre>

<p>With that out of the way, here's the interesting part of this
example. We're going to define a resource which renders a whole-year
calendar. The year it will render the calendar for will be the year in
the request URL. So, for example, <code>/2009</code> will render a
calendar for 2009. First, here's a resource that renders a calendar
for the year passed to its initializer:</p>

<pre class="python">
from calendar import calendar

class YearPage(Resource):
    def __init__(self, year):
        Resource.__init__(self)
        self.year = year

    def render_GET(self, request):
        return "&lt;html&gt;&lt;body&gt;&lt;pre&gt;%s&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;" % (calendar(self.year),)
</pre>

<p>Pretty simple - not all that different from the first dynamic resource
demonstrated in <a href="dynamic-content.xhtml">Generating a Page
Dynamically</a>. Now here's the resource that handles URLs with a year in them
by creating a suitable instance of this <code>YearPage</code> class:</p>

<pre class="python">
class Calendar(Resource):
  def getChild(self, name, request):
      return YearPage(int(name))
</pre>

<p>By implementing <code class="API"
base="twisted.web.resource.Resource">getChild</code> here, we've just defined
how Twisted Web should find children of <code>Calendar</code> instances when
it's resolving an URL into a resource. This implementation defines all integers
as the children of <code>Calendar</code> (and punts on error handling, more on
that later).</p>

<p>All that's left is to create a <code>Site</code> using this resource as its
root and then start the reactor:</p>

<pre>
root = Calendar()
factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()
</pre>

<p>And that's all. Any resource-based dynamic URL handling is going to look
basically like <code>Calendar.getChild</code>. Here's the full example code:</p>

<pre class="python">
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor

from calendar import calendar

class YearPage(Resource):
    def __init__(self, year):
        Resource.__init__(self)
        self.year = year

    def render_GET(self, request):
        return "&lt;html&gt;&lt;body&gt;&lt;pre&gt;%s&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;" % (calendar(self.year),)

class Calendar(Resource):
  def getChild(self, name, request):
      return YearPage(int(name))

root = Calendar()
factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()
</pre>


</body>
</html>
